﻿using AngleSharp.Dom;
using Masuit.Tools;
using Masuit.Tools.Database;
using Models.Common;
using Models.Device;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Crypto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;

namespace ServiceWeb.WebClient
{
    public class ServiceClientPlainItem
    {


        public DataTable getPlainItemValue(string pguid,string modelno)
        {
            EFYihongIotContext ef = new EFYihongIotContext();


            var plianitems = (from l in ef.iot_device_task_plain
                        join c in ef.iot_device_task_plain_item on l.tp_guid equals c.tpi_tp_guid
                        where l.tp_p_guid == pguid
                        select c);

            //下面这样查会慢点
           // var tpguids=ef.iot_device_task_plain.Where(c=>c.tp_p_guid==pguid).Select(c=>c.tp_guid).ToList();
           // var plianitems = ef.iot_device_task_plain_item.Where(c => tpguids.Contains(c.tpi_tp_guid));

            if (!string.IsNullOrEmpty(modelno))
            {
                string[] id_str = modelno.Split(',');
                //string[]转换为int[]
                int[] id_int = Array.ConvertAll<string, int>(id_str, delegate (string s) { return int.Parse(s); });
                if (id_int.Length > 0)
                {
                    plianitems = plianitems.Where(i => id_int.Contains((int)i.tpi_p_model_no));
                }
            }
            var result = plianitems.Select(x => new { x.tpi_guid, x.tpi_p_model_no, x.tpi_complete, x.tpi_key, x.tpi_value });
           return plianitems.ToDataTable();
        }


    }
}
